//
//  XXMathFuntion.swift
//  MOMAEducation
//
//  Created by Rdxer on 2017/11/27.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit

struct XXMathLibrary {
    
    /// 限制只能在此区间
    public static func absLimit(_ value:CGFloat,min:CGFloat,max:CGFloat) -> CGFloat{
        
        if value < min{
            return min
        }
        
        if value > max{
            return max
        }
        
        return value
    }
    
    /// 通道转换
    public static func channelConversion(_ value:CGFloat,inputMin:CGFloat,inputMax:CGFloat,outputMin:CGFloat,outputMax:CGFloat,isStrictMode:Bool = false) -> CGFloat{
        // 归零
        let absValue = value - inputMin
        
        if isStrictMode && absValue < 0.000001{
            return outputMin
        }
        
        // 输入区间
        let inputRange = inputMax - inputMin
        // 输入区间的游标比例
        let positionScale = absValue / inputRange
        
        // 输出区间
        let outputRange = outputMax - outputMin
        // 输出区间的游标值
        let outAbsValue = outputRange * positionScale
        
        
        let returnValue = outAbsValue + outputMin
        
        if isStrictMode && outAbsValue < 0.000001{
            return outputMin
        }
        
        return returnValue
    }
}
